import dayjs from 'dayjs';
import { useTable, LIST_TYPE } from '@crud/index';
import { getList } from '@/api/crm-dyj/user';
import { toolOptions2Map } from '@/utils';

// 查询

// 注册终端
export const registerOptions = [
  { label: '不限', value: '' },
  { label: 'PC', value: 1 },
  { label: '小程序', value: 2 }
];
// 会员类别
export const vipLevelOptions = [
  { label: '不限', value: '' },
  { label: '无会员', value: 99 },
  { label: '试用会员', value: 0 },
  { label: '月卡VIP', value: 10 },
  { label: '月卡SVIP', value: 15 },
  { label: '年卡VIP', value: 20 },
  { label: '年卡SVIP', value: 30 },
  { label: '至尊会员(10人版)', value: 31 },
  { label: '至尊会员(20人版)', value: 32 },
  { label: '至尊会员(30人版)', value: 33 },
  { label: '至尊会员(40人版)', value: 34 },
  { label: '至尊会员(50人版)', value: 35 },
  { label: '至尊会员(80人版)', value: 36 },
  { label: '至尊会员(100人版)', value: 37 }
];
/** 快速日期枚举 */
export const quickDateMap = [
  {
    label: '3天',
    value: [3, 'd']
  },
  {
    label: '5天',
    value: [5, 'd']
  },
  {
    label: '7天',
    value: [7, 'd']
  },
  {
    label: '10天',
    value: [10, 'd']
  },
  {
    label: '15天',
    value: [15, 'd']
  },
  {
    label: '一月',
    value: [1, 'month']
  },
  {
    label: '一季',
    value: [3, 'month']
  },
  {
    label: '半年',
    value: [6, 'month']
  },
  {
    label: '一年',
    value: [1, 'year']
  }
];
// 用户类型
export const leaderOptions = [
  { label: '不限', value: '' },
  { label: '团队管理员', value: 1 },
  { label: '团队成员', value: 0 }
];
// 用户标签
export const userLabelOptions = [
  { label: '不限', value: 0 },
  { label: '近7天新增用户', value: 1 },
  { label: '近7天活跃用户', value: 2 },
  { label: '近7天未活跃用户', value: 3 }
];

export const registerMap = toolOptions2Map(registerOptions);
export const leaderMap = toolOptions2Map(leaderOptions);
export const vipLevelMap = toolOptions2Map(vipLevelOptions);

// 表格
export const useTableList = (options: any = {}) => {
  const columns = [
    { label: '手机号', prop: 'mobile', minWidth: 130, fixed: 'left' },
    { label: '身份', prop: 'leader', width: 120, render: (s: any) => leaderMap[s.row.leader] || '-' },
    { label: '用户昵称', prop: 'nick_name', width: 120, showOverflowTooltip: true },
    { label: '注册时间', prop: 'register_time', width: 177, type: 'dateTime' },
    { label: '注册终端', prop: 'register_type', width: 100, render: (s: any) => registerMap[s.row.register_type] || '-' },
    { label: '1级渠道', prop: 'xsource', width: 120 },
    { label: '2级渠道', prop: 'xterm', width: 120 },
    { label: '讲师名称', prop: 'agent_name', minWidth: 120 },
    { label: '会员类别', prop: 'vip_level', width: 120, render: (s: any) => vipLevelMap[s.row.vip_level] || '无会员' },
    { label: '团队人数', prop: 'team_member_num', width: 100, render: ({ row: { team_member_num } }: any) => (team_member_num < 99 ? team_member_num : '-') },
    { label: '团队ID', prop: 'team_id', width: 120 },
    { label: '团队名称', prop: 'team_title', minWidth: 160, showOverflowTooltip: true },
    { label: '加入时间', prop: 'join_team_time', width: 177, type: 'dateTime' },
    { label: '分组', prop: 'group_title', width: 120 },
    { label: '会员到期时间', prop: 'vip_expire_time', width: 177, type: 'dateTime' },
    { label: '最后访问时间', prop: 'last_access_time', width: 177, type: 'dateTime' },
    { label: '操作', align: 'center', render: 'actions', width: 160, fixed: 'right' }
  ];

  return useTable({
    ...options,
    type: LIST_TYPE.DYJ,
    params: {
      _dateRange: [],
      mobile: undefined,
      register_type: undefined,
      xsource: undefined,
      xterm: undefined,
      vip_level: undefined,
      leader: undefined,
      user_label: 0,
      // times
      reg_start_time: undefined,
      reg_end_time: undefined,
      login_start_time: undefined,
      login_end_time: undefined,
      login_not_start_time: undefined,
      login_not_end_time: undefined
    },
    columns: columns,
    getParams: ({ _dateRange, ...params }) => {
      if (_dateRange?.length > 0) {
        const [start, end] = _dateRange ?? [];
        /** 清空用户标签字段 */
        params = {
          ...params,
          reg_start_time: undefined,
          reg_end_time: undefined,
          login_start_time: undefined,
          login_end_time: undefined,
          login_not_start_time: undefined,
          login_not_end_time: undefined
        };
        params.start_time = dayjs(start).startOf('d').format('X');
        params.end_time = dayjs(end).endOf('d').format('X');
      }
      if (params.user_label > 0) {
        const [startTime, endTime] = [dayjs().subtract(1, 'week').startOf('d').format('X'), dayjs().endOf('d').format('X')];
        const userLabel = params.user_label;
        if (userLabel === 1) {
          params.reg_start_time = startTime;
          params.reg_end_time = endTime;
        } else if (userLabel === 2) {
          params.login_start_time = startTime;
          params.login_end_time = endTime;
        } else if (userLabel === 3) {
          params.login_not_start_time = startTime;
          params.login_not_end_time = endTime;
        }
        /** 用户标签日期与注册时间互斥，需要清空 */
        params.start_time = undefined;
        params.end_time = undefined;
      }
      params.no_vip_time = dayjs().format('X');
      return params;
    },
    getList: getList
  });
};
